/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package br.ufrj.dcc.pushpeer.dht;

import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
	
	private static MessageDigest m;
	
    private MD5(){
		//Private constructor to suppress instantiation.
		throw new AssertionError();
    }
    
    public static long valueMD5(long value) {
    	if(m == null) {
    		try {
    			m = MessageDigest.getInstance("MD5");
    		}
    		catch (NoSuchAlgorithmException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	ByteBuffer buffer = ByteBuffer.allocate(8);
    	buffer.putLong(value);
    	byte[] valueInBytes = buffer.array();
    	m.update(valueInBytes,0,valueInBytes.length);
    	return Math.abs((new BigInteger(1,m.digest())).longValue());
    }
}